--
-- Author: shunguo.chen
-- Date: 2018-11-19 11:43:01
--
-- TeamTargetThirdRender 组队查找器三级菜单
--

local ClassRef = app.mvc.createRenderClass(...)

local COLOR_SELECTED = cc.c3b( 255, 225, 132 )
local COLOR_UNSELECTED_LOCK = cc.c3b( 97, 97, 97 )
local COLOR_UNSELECTED_OPEN = cc.c3b( 227, 184, 109 )

--data = TeamTargetVo
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.TeamTargetNode_3))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    self._unselectColor = COLOR_UNSELECTED_LOCK

    self:setContentSize(self._ui.Panel:getContentSize())
    self._ui.Panel:setTouchEnabled(true)
    display.uiAddClick(self._ui.Panel, function()
        executeFunc(self._onClick, self, self:isSelected(), self._data)
    end)
    self:refresh()
end

function ClassRef:refresh()
    if self._data then
        self._ui.name:setString(self._data:getDifficultyName())

        local isOpen = self._data:isOpen()
        self._ui.btn_bg_1:setVisible(isOpen)
        self._ui.btn_bg_2:setVisible(not isOpen)
        self._ui.icon_lock:setVisible(not isOpen)
        self._unselectColor = isOpen and COLOR_UNSELECTED_OPEN or COLOR_UNSELECTED_LOCK

        self:refreshAutoRecruit()
        self:refreshAutoApply()
        self:refreshInCrossMatch()
    end
end

function ClassRef:setSelected(b)
    self._ui.btn_bg_select:setVisible(b)

    local color = b and COLOR_SELECTED or self._unselectColor
    self._ui.name:setTextColor(color)
end

function ClassRef:isSelected()
    return self._ui.btn_bg_select:isVisible()
end

-- 自动招募标记，已经有队伍可能出现，只有一个
function ClassRef:refreshAutoRecruit()
    self._ui.icon_1:setVisible(self._data:isAutoRecruit())
end

-- 自动申请标记，没有队伍时可能出现，可以有多个
function ClassRef:refreshAutoApply()
    self._ui.icon_2:setVisible(self._data:isAutoApply())
end

-- 跨服匹配
function ClassRef:refreshInCrossMatch()
    self._ui.icon_3:setVisible(self._data:isInCrossMatch())
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
